Skip to content

Fix crash caused by USB device disconnection during permission grant - #86

Open
KairuiLiu wants to merge 1 commit into
alexey-pelykh:mainfrom
KairuiLiu:main
Open

Fix crash caused by USB device disconnection during permission grant#86
KairuiLiu wants to merge 1 commit into
alexey-pelykh:mainfrom
KairuiLiu:main

Conversation

@KairuiLiu

Copy link
Copy Markdown

Summary

  • Fix a race condition crash (~5% repro rate) when rapidly plugging/unplugging USB devices. The permission dialog may grant access to a device that has already been physically disconnected, causing UsbManager.openDevice() to return null and getSerialNumber() to throw an uncaught SecurityException, killing the process.
  • Add null-checks and exception guards at four critical points in USBMonitor and adapt the Flutter plugin caller accordingly.

Root Cause

When a USB device is unplugged while the system permission dialog is still showing, the following race occurs:

T1: Device attached → requestPermission() → system dialog shown
T2: Device physically unplugged → /dev/bus/usb/xxx removed
T3: User taps "Allow" → BroadcastReceiver gets EXTRA_PERMISSION_GRANTED=true
T4: processConnect() posts to mAsyncHandler (async delay widens the race window)
T5: UsbControlBlock constructor → openDevice() returns null (device gone)
T6: updateDeviceInfo() → getSerialNumber() → SecurityException → FATAL

Changes

File Change
USBMonitor.javaUsbControlBlock() Check openDevice() return value; throw IOException if null instead of proceeding to getSerialNumber()
USBMonitor.javaupdateDeviceInfo() Wrap getSerialNumber()/getManufacturerName()/getProductName() in try-catch for SecurityException; early-return if openDevice() returns null
USBMonitor.javaprocessConnect() Catch IOException from UsbControlBlock constructor, log warning, and call processCancel() to notify listeners gracefully
USBMonitor.javaopenDevice() Add throws IOException to method signature
UvcCameraPlatform.java Adapt to new IOException on openDevice(), wrap as IllegalStateException

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant